/*!
 * \file __frhashmap_varfield_show.c
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.\n
 *
 * \version 3.0
 * \author geyh
 * \date 2016/03/31
 *
 */
#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "frlist.h"
#include "frhash2.h"
#include "conntrack.h"
#include "frct_comm.h"

#include "apmtest_comm.h"

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}

extern void __frhashmap_varfield_show(Frlist_node *p_st_node,char **pp_outbuff);
extern void __frhashmap_varfield_free(Frlist_node *p_st_node);

static void __case1(void)
{
    Frlist_node *p_st_node = NULL;
    char *p_outbuff = NULL;


    __frhashmap_varfield_show(p_st_node,&p_outbuff);

    CU_ASSERT_PTR_NULL(p_outbuff);
    return;
}

static void __case2(void)
{
    Frlist_node *p_st_node = NULL;
    char *p_outbuff = NULL;

    p_st_node = (Frlist_node *)frmalloc(sizeof(Frlist_node));
    p_st_node->data = NULL;

    __frhashmap_varfield_show(p_st_node,&p_outbuff);

    CU_ASSERT_PTR_NULL(p_outbuff);
    frfree(p_st_node);
    return;
}

static void __case3(void)
{
    Frlist_node *p_st_node = NULL;
    Frvar_field *p_st_field = NULL;
    int i_tmp_size = 0;
    char *p_tmp = NULL;
    char *p_outbuff = NULL;

    p_st_field = (Frvar_field *)frmalloc(sizeof(Frvar_field));
    memset(p_st_field,'\0',sizeof(Frvar_field));

    i_tmp_size = strlen("case3") + 1;
    p_tmp = (char *)frmalloc(i_tmp_size);
    snprintf(p_tmp,i_tmp_size,"%s","case3");
    p_st_field->p_field_name = p_tmp;

    p_st_field->em_data_type = FRINT;
    p_st_field->field_value.i_value = 100;

    p_st_node = frlist_node_create(p_st_field);

    __frhashmap_varfield_show(p_st_node,&p_outbuff);

    CU_ASSERT_PTR_NOT_NULL(p_outbuff);
    CU_ASSERT_STRING_EQUAL(p_outbuff,"field_name = case3 em_data_type = FRINT i_value = 100\n");
    __frhashmap_varfield_free(p_st_node);

    frfree(p_st_node);
    frfree(p_outbuff);
    return;
}

static void __case4(void)
{
    Frlist_node *p_st_node = NULL;
    Frvar_field *p_st_field = NULL;
    int i_tmp_size = 0;
    char *p_tmp = NULL;
    char *p_outbuff = NULL;

    p_st_field = (Frvar_field *)frmalloc(sizeof(Frvar_field));
    memset(p_st_field,'\0',sizeof(Frvar_field));

    i_tmp_size = strlen("case4") + 1;
    p_tmp = (char *)frmalloc(i_tmp_size);
    snprintf(p_tmp,i_tmp_size,"%s","case4");
    p_st_field->p_field_name = p_tmp;

    p_st_field->em_data_type = FRSTRING;
    i_tmp_size = strlen("p_value") + 1;
    p_tmp = (char *)frmalloc(i_tmp_size);
    snprintf(p_tmp,i_tmp_size,"%s","p_value");
    p_st_field->field_value.p_value = p_tmp;

    p_st_node = frlist_node_create(p_st_field);

    __frhashmap_varfield_show(p_st_node,&p_outbuff);

    CU_ASSERT_PTR_NOT_NULL(p_outbuff);
    CU_ASSERT_STRING_EQUAL(p_outbuff,"field_name = case4 em_data_type = FRSTRING p_value = p_value\n");

    __frhashmap_varfield_free(p_st_node);

    frfree(p_st_node);
    frfree(p_outbuff);
    return;
}

static void __case5(void)
{
    Frlist_node *p_st_node = NULL;
    Frvar_field *p_st_field = NULL;
    int i_tmp_size = 0;
    char *p_tmp = NULL;
    char *p_outbuff = NULL;
    Frbindata *p_st_value = NULL;
    p_st_field = (Frvar_field *)frmalloc(sizeof(Frvar_field));
    memset(p_st_field,'\0',sizeof(Frvar_field));

    i_tmp_size = strlen("case5") + 1;
    p_tmp = (char *)frmalloc(i_tmp_size);
    snprintf(p_tmp,i_tmp_size,"%s","case5");
    p_st_field->p_field_name = p_tmp;

    p_st_field->em_data_type = FRCLOB;
    p_tmp = (char *)frmalloc(4);
    memcpy(p_tmp,"\x65\x00\x65\x00",4);

    p_st_value = (Frbindata *)frmalloc(sizeof(Frbindata));
    p_st_value->p_data = p_tmp;
    p_st_value->ui_len = 4;
    p_st_field->field_value.p_st_value = p_st_value;

    p_st_node = frlist_node_create(p_st_field);

    __frhashmap_varfield_show(p_st_node,&p_outbuff);
    CU_ASSERT_PTR_NOT_NULL(p_outbuff);
    CU_ASSERT_STRING_EQUAL(p_outbuff,"field_name = case5 em_data_type = FRCLOB p_st_value->p_data = 65 00 65 00                                      e.e.\n\n");
    __frhashmap_varfield_free(p_st_node);

    frfree(p_st_node);
    frfree(p_outbuff);
    return;
}

static void __case6(void)
{
    Frlist_node *p_st_node = NULL;
    Frvar_field *p_st_field = NULL;
    int i_tmp_size = 0;
    char *p_tmp = NULL;
    char **pp_outbuff = NULL;

    p_st_field = (Frvar_field *)frmalloc(sizeof(Frvar_field));
    memset(p_st_field,'\0',sizeof(Frvar_field));

    i_tmp_size = strlen("case3") + 1;
    p_tmp = (char *)frmalloc(i_tmp_size);
    snprintf(p_tmp,i_tmp_size,"%s","case3");
    p_st_field->p_field_name = p_tmp;

    p_st_field->em_data_type = FRINT;
    p_st_field->field_value.i_value = 100;

    p_st_node = frlist_node_create(p_st_field);

    __frhashmap_varfield_show(p_st_node,pp_outbuff);

    CU_ASSERT_PTR_NULL(pp_outbuff);
    __frhashmap_varfield_free(p_st_node);

    frfree(p_st_node);
    return;
}

int add_suite___frhashmap_varfield_show(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite("frhash2 __frhashmap_varfield_show()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    CU_add_test(pSuite, "case1", __case1);
    CU_add_test(pSuite, "case2", __case2);
    CU_add_test(pSuite, "case3", __case3);
    CU_add_test(pSuite, "case4", __case4);
    CU_add_test(pSuite, "case5", __case5);
    CU_add_test(pSuite, "case6", __case6);

    return FR_SUCC;
}
